﻿using DBHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Util.SystemModel.System;

namespace Util.ServiceDAO.User
{
    public class MenuDAO
    {
        public static int InsertMenu(MenuModel mm)
        {
            string cmdText = @"INSERT INTO T_Sys_Menu
			(MenuId
			,CreateId
			,CreateTime
			,UpdateId
			,UpdateTime
			,Status
			,Title
			,Hierarchy
			,ParentId
			,Queue
			,Icon
			,Href
						)
			VALUES(@MenuId
			,@CreateId
			,@CreateTime
			,@UpdateId
			,@UpdateTime
			,@Status
			,@Title
			,@Hierarchy
			,@ParentId
			,@Queue
			,@Icon
			,@Href
						);SELECT CAST(scope_identity() AS int)";
            SqlParameter[] paras = new SqlParameter[] {
                new SqlParameter("@MenuId", mm.MenuId)
                ,new SqlParameter("@CreateId", mm.CreateId)
                ,new SqlParameter("@CreateTime", mm.CreateTime)
                ,new SqlParameter("@UpdateId", mm.UpdateId)
                ,new SqlParameter("@UpdateTime", mm.UpdateTime)
                ,new SqlParameter("@Status", mm.Status)
                ,new SqlParameter("@Title", mm.Title)
                ,new SqlParameter("@Hierarchy", mm.Hierarchy)
                ,new SqlParameter("@ParentId", mm.ParentId)
                ,new SqlParameter("@Queue", mm.Queue)
                ,new SqlParameter("@Icon", mm.Icon)
                ,new SqlParameter("@Href", mm.Href)
                            };
            return Convert.ToInt32(DatabaseFactory.ExecuteScalar(cmdText, CommandType.Text, paras));
        }

        public static int UpdateMenu(MenuModel mm)
        {
            string cmdText = @"UPDATE T_Sys_Menu SET  MenuId=@MenuId
			,CreateId=@CreateId
			,CreateTime=@CreateTime
			,UpdateId=@UpdateId
			,UpdateTime=@UpdateTime
			,Status=@Status
			,Title=@Title
			,Hierarchy=@Hierarchy
			,ParentId=@ParentId
			,Queue=@Queue
			,Icon=@Icon
			,Href=@Href
			Where MenuID=@MenuID ";
            SqlParameter[] paras = new SqlParameter[] {
                new SqlParameter("@MenuId", mm.MenuId)
                ,new SqlParameter("@CreateId", mm.CreateId)
                ,new SqlParameter("@CreateTime", mm.CreateTime)
                ,new SqlParameter("@UpdateId", mm.UpdateId)
                ,new SqlParameter("@UpdateTime", mm.UpdateTime)
                ,new SqlParameter("@Status", mm.Status)
                ,new SqlParameter("@Title", mm.Title)
                ,new SqlParameter("@Hierarchy", mm.Hierarchy)
                ,new SqlParameter("@ParentId", mm.ParentId)
                ,new SqlParameter("@Queue", mm.Queue)
                ,new SqlParameter("@Icon", mm.Icon)
                ,new SqlParameter("@Href", mm.Href)
                };
            return DatabaseFactory.ExecuteNonQuery(cmdText, CommandType.Text, paras);
        }

        public static List<MenuModel> SelectMenuByWhere(string where, SqlParameter[] paras, string queue)
        {
            DataTable dt = new DataTable();
            List<MenuModel> list = new List<MenuModel>();
            string cmdText = "SELECT * FROM T_Sys_Menu WHERE 1 = 1 {0}{1}";
            cmdText = string.Format(cmdText, where, queue);
            dt = DatabaseFactory.ExecuteDataset(cmdText, CommandType.Text, paras).Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                MenuModel mm = new MenuModel();
                mm.MenuId = dr["MenuId"].ToString();
                mm.CreateId = dr["CreateId"].ToString();
                mm.CreateTime = dr["CreateTime"].ToString();
                mm.UpdateId = dr["UpdateId"].ToString();
                mm.UpdateTime = dr["UpdateTime"].ToString();
                mm.Status = dr["Status"].ToString();
                mm.Title = dr["Title"].ToString();
                mm.Hierarchy = dr["Hierarchy"].ToString();
                mm.ParentId = dr["ParentId"].ToString();
                mm.Queue = dr["Queue"].ToString();
                mm.Icon = dr["Icon"].ToString();
                mm.Href = dr["Href"].ToString();
                list.Add(mm);
            }
            return list;
        }

        public static MenuModel GetMenuModel(string MenuID)
        {
            string where = " and MenuID=@MenuID";
            SqlParameter[] paras = new SqlParameter[] {
             new SqlParameter("@MenuID", MenuID)
             };
            return SelectMenuByWhere(where, paras, "").FirstOrDefault();
        }

        public static bool UpdateMenuStatus(string MenuID, string status, int userId)
        {
            var list = GetMenuModel(MenuID);
            if (list != null)
            {
                list.Status = status;
                list.UpdateTime = DateTime.Now.ToString();
                list.UpdateId = userId.ToString();
                if (UpdateMenu(list).Equals(1))
                    return true;
                return false;
            }
            else
            {
                return false;
            }
        }
    }
}
